<template>
  <eagle-list ref="eagleList" showTab :tabData="tabData" @bindData="bindData">
    <div>
      <eagle-cell class="card" v-for="(item, index) in list" :key="index">
        <div>
          <eagle-row>
            <eagle-col span="24">
              {{ index + 1 }}、<b>{{ item.CheckListDetailItem }}</b>
            </eagle-col>
            <eagle-col span="24">
              <input style="width:1.5rem" v-if="item.NeedInput" placeholder="kg"></input>
            </eagle-col>
          </eagle-row>
          <eagle-divider />
          <eagle-row gutter="20" class="dep_sort">
            <div v-if="ctCode == null">
              <eagle-col span="8">
                <eagle-button :color="item.CheckResult == 'Y' && item.checked ? '#07c160' : ''
                  " class="submit-check" type="default" @click.stop="handleClick(index, 'Y')">符合
                </eagle-button>
              </eagle-col>
              <eagle-col span="8">
                <eagle-button :color="item.CheckResult == 'N' && item.checked ? '#ff0000' : ''
                  " class="submit-check" type="default" @click.stop="handleClick(index, 'N')">不符合
                </eagle-button>
              </eagle-col>
              <eagle-col span="8">
                <eagle-button :color="item.CheckResult == 'NA' && item.checked ? '#ec971f' : ''
                  " class="submit-check" type="default" @click.stop="handleClick(index, 'NA')">不适用
                </eagle-button>
              </eagle-col>
            </div>
            <div v-if="ctCode != null">
              <eagle-col span="8">
                <eagle-button :color="item.CheckResult == 'Y' ? '#07c160' : ''" class="submit-check" type="default"
                  @click.stop="handleClick(index, 'Y')">符合
                </eagle-button>
              </eagle-col>
              <eagle-col span="8">
                <eagle-button :color="item.CheckResult == 'N' ? '#ff0000' : ''" class="submit-check" type="default"
                  @click.stop="handleClick(index, 'N')">不符合
                </eagle-button>
              </eagle-col>
              <eagle-col span="8">
                <eagle-button :color="item.CheckResult == 'NA' ? '#ec971f' : ''" class="submit-check" type="default"
                  @click.stop="handleClick(index, 'NA')">不适用
                </eagle-button>
              </eagle-col>
            </div>
          </eagle-row>
          <eagle-form v-if="item.CheckResult == 'N' && ctCode == null">
            <div class="inconformity-list">
              <eagle-input rows="3" class="input_text" name="uploader" label="隐患图片">
                <template #input>
                  <eagle-image-uploader :images.sync="item.Attachs"></eagle-image-uploader>
                </template>
              </eagle-input>
              <eagle-input label="隐患描述" v-model="item.HiddenDangerDesc" rows="3" autosize isInputText type="textarea"
                placeholder="隐患描述"></eagle-input>
              <div v-if="VUE_APP_PLANTFORM && VUE_APP_PLANTFORM == 'CY'">
                <eagle-input label="风险等级" rows="3" name="radio">
                  <template #input>
                    <van-radio-group v-model="list[index].RiskLevel" direction="vertical">
                      <van-radio name="1" style="margin: 0 10px 10px 0">低风险</van-radio>
                      <van-radio name="2" style="margin: 0 10px 10px 0">中风险</van-radio>
                      <van-radio name="3" style="margin: 0 10px 10px 0">高风险</van-radio>
                    </van-radio-group>
                  </template>
                </eagle-input>
                <van-field class="input_text" v-model="list[index].TypeFullName" label="隐患分类" placeholder="请选择" required
                  readonly @click="setCurrentIndex(index)">
                  <!-- <van-field class="input_text" v-model="list[index].LGDFullName" label="隐患分类" placeholder="请选择" required readonly @click="showDangerTypeDialog = true"> -->
                  <template #right-icon>
                    <van-icon name="arrow" />
                  </template>
                </van-field>
                <!-- <van-field class="input_text" :value="list[index].correctiveFullName" label="整改部门/人" placeholder="请选择" required readonly @click="setCurrentIndex2(index)">
                        <template #right-icon>
                            <van-icon name="arrow" />
                        </template>
              </van-field> -->
                <eagle-input isInputText name="CheckNames" v-model="list[index].CorrectiveChnName" label="整改人"
                  :autosize="true" :islink="true" :rows="1" type="textarea" required readonly placeholder="请选择"
                  @click="setCurrentIndex2(index)"></eagle-input>
                  <eagle-date
                label="整改期限"
                required
                :IsCurrentTime="false"
                :date.sync="list[index].CorrectiveDate"
              ></eagle-date>
              </div>
            </div>
          </eagle-form>
          <eagle-form v-if="ctCode != null && item.CheckResult == 'N'">
            <div class="inconformity-list">
              <eagle-input label="点检状sss态" v-model="item.Status == '10' ? '未整改' : '已整改'" readonly></eagle-input>
              <eagle-input class="input_text" label="隐患图片" :value="item.Attachs" readonly>
                <template #input>
                  <eagle-image-preview :images="item.Attachs">
                  </eagle-image-preview>
                </template>
              </eagle-input>
              <eagle-input label="隐患描述" v-model="item.HiddenDangerDesc" rows="3" autosize readonly
                type="textarea"></eagle-input>
                <div v-if=" VUE_APP_PLANTFORM && VUE_APP_PLANTFORM == 'CY'">
                <eagle-input label="风险等级" :value="dueRiskLeve(item.RiskLevel)" autosize readonly
                  type="textarea"></eagle-input>
                <eagle-input label="隐患分类" v-model="item.TypeFullName" autosize readonly type="textarea"></eagle-input>
                <eagle-input label="整改人" v-model="item.CorrectiveChnName" type="textarea"></eagle-input>
              </div>
              <eagle-input rows="3" class="input_text" name="uploader" label="整改图片">
                <template #input>
                  <eagle-image-preview :images="item.VerifyAttachs">
                  </eagle-image-preview>
                </template>
              </eagle-input>
              <eagle-input label="整改说明" v-model="item.VerifyRemarks" rows="3" autosize readonly
                type="textarea"></eagle-input>
                <eagle-input v-if=" VUE_APP_PLANTFORM && VUE_APP_PLANTFORM == 'CY'" label="整改期限" v-model="item.CorrectiveDate" type="textarea"></eagle-input>
            </div>
          </eagle-form>
        </div>
      </eagle-cell>
      <template>
        <div class="btn_submit">
          <eagle-button class="submit-button" block type="info" square v-if="ctCode == null"
            @click="SaveCheckDetails('0')">提交</eagle-button>
        </div>
      </template>
      <!-- 隐患分类 -->
      <danger-type-dialog v-model="showDangerTypeDialog" @callback="onDangerTypeCallback">
      </danger-type-dialog>
      <!--人员弹框-->
      <user-choose :show.sync="userChooseDialogShow" :selectedValue="selectedValue" @callback="userChooseCallback"
        :isMulti="false" :rowIndex="rowIndex">
      </user-choose>
    </div>
  </eagle-list>
</template>

<script>
import { getDate } from "@/utils/getDate";
import userChoose from "@/views/components/user/userChooseDialog.vue";
import { mapGetters } from "vuex";
import DangerTypeDialog from "@/views/components/danger/dangerTypeDialog7.vue";
import DepartmentAndUserDialog from "@/views/components/department/departmentUserChooseDialog.vue";
import { GetDetail, GetAbnormal } from "@/api/siteFireEquipment";
import { List } from "vant";
import store from "@/store/index";
export default {
  components: {
    userChoose,
    DangerTypeDialog,
    DepartmentAndUserDialog,
  },
  computed: {
    ...mapGetters(["CompanyCode"]),
  },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      active: 0,
      list: [],
      mainCode: this.$route.query.mainCode,
      objCode: this.$route.query.objCode,
      ctCode: this.$route.query.ctCode,
      // name: this.$route.query.name,
      // tabData: [
      //   {
      //     title: "点检",
      //     url: turl,
      //     params: {
      //       mainCode: this.$route.query.mainCode,
      //       objCode: this.$route.query.objCode,
      //     },
      //     isPage: false,
      //   },
      // ],
      // showTab: false,
      tabData: [],
      userChooseDialogShow: false,
      userTagData: [],
      index: "",
      saveOp: "",
      keyObject: {},
      formData: [],
      Status: "",
      selectedValue: "",
      rowIndex: "0",
      id: "",
      CheckListDetailItem: "",
      showDangerTypeDialog: false,
      showDepartmentAndUserDialog: false,
      VUE_APP_PLANTFORM: process.env.VUE_APP_PLANTFORM,
      CompanyCode2: "",
      currentIndex: 0,
      form: {},
    };
  },
  created() {
    if (this.ctCode == null) {
      this.tabData = [
        {
          title: "点检",
          url: "/SiteCheckListDetail/GetListByCode",
          params: {
            mainCode: this.$route.query.mainCode,
            objCode: this.$route.query.objCode,
          },
          isPage: false,
        },
      ];
    } else {
      this.tabData = [
        {
          title: "点检记录",
          url: "/SiteCheckTask/GetListDetail",
          params: {
            ctCode: this.$route.query.ctCode,
            objCode: this.$route.query.objCode,
          },
          isPage: false,
        },
      ];
    }
    if(this.objCode){
      this.geDetail();
    }
    if(process.env.VUE_APP_PLANTFORM&&process.env.VUE_APP_PLANTFORM=="CY"){
      store.state.title="专项清单[点检]"
    }
    this.id=this.$route.query.id
  },
  methods: {
    geDetail() {
      let params = { code: this.objCode };
      GetDetail(params).then((res) => {
        // this.form = res.data;
        this.CompanyCode2 = res.data.CompanyCode;
      });
    },
    getDate(data) {
      return getDate(data);
    },
    bindData(data) {
      this.bindData1(data);
    },
    bindData1(data) {
      if (this.ctCode != null) {
        this.list = data;
      } else {
        let newList = [];
        let Status = data.Status;
        data.map((item, index) => {
          item.Status = Status;
          item.CheckResult = "Y";
          item.checked = item.CheckResult;
          // item.Attachs=this.baseUrl+item.Attachs
          // console.log("22222222")
          // console.log(item.Attachs)
          // item.VerifyAttachs=this.baseUrl+item.VerifyAttachs
          newList.push(item);
        });
        this.list = newList;
      }
    },
    showUser(index, item) {
      this.selectedValue = item.CorrectiveResponsibleUserName;
      this.rowIndex = index;
      this.keyObject = {
        index,
      };
      this.userChooseDialogShow = true;
    },
    //点击正常、异常
    handleClick(index, CheckResult) {
      if (
        !(this.ctCode == "" || this.ctCode == null || this.ctCode == undefined)
      ) {
        return false;
      }
      this.list.map((item, idx) => {
        if (index == idx) {
          if (item.CheckResult == CheckResult) {
            item.checked = !item.checked;
            item.CheckResult = "";
          } else {
            item.checked = true;
            item.CheckResult = CheckResult;
          }
        }
      });
    },
    // 保存
    SaveCheckDetails(saveOp) {
      this.saveOp = saveOp;
      if (saveOp == "0") {
        this.Status = 10;
      }
      let newArr = [];
      var message = "";
      var index = 1;
      this.list.map((item1) => {
        if (item1.CheckResult == "N" && saveOp == "0") {
          if (!item1.Attachs && this.VUE_APP_PLANTFORM && this.VUE_APP_PLANTFORM != "CY") {
            message =
              message + "[" + item1.Order + "] 的隐患图片未选择不能提交 ";
          }
          if (!item1.HiddenDangerDesc) {
            message =
              message + "[" + item1.Order + "] 的隐患描述未填写不能提交 ";
          }
          if ( this.VUE_APP_PLANTFORM && this.VUE_APP_PLANTFORM == "CY") {
            if (!item1.RiskLevel) {
              message =
                message + "[" + item1.Order + "] 的风险等级未填写不能提交 ";
            }
            if (!item1.TypeFullName) {
              message =
                message + "[" + item1.Order + "] 的隐患分类未填写不能提交 ";
            }
            if (!item1.CorrectiveChnName) {
              message =
                message + "[" + item1.Order + "] 的整改部门/人未选择不能提交 ";
            }
            if (!item1.CorrectiveDate) {
              message =
                message + "[" + item1.Order + "] 的整改期限未选择不能提交 ";
            }
          }
        }
        newArr.push(item1);
      });
      if (message) {
        return this.$toast(message);
      }
      const hasResult = newArr.filter((v) => v.CheckResult);
      const noResult = newArr.filter((v) => !v.CheckResult && !v.checked);
      if (hasResult.length == 0) {
        this.$toast("未包含任何需暂存/提交的点检记录");
        return;
      }
      let formData = [];
      newArr.map((item) => {
        if (item.CheckResult) {
          formData.push(item);
        }
      });

      this.formData = formData;
      let params = {
        objCode: this.$route.query.objCode,
        companyCode: this.CompanyCode,
        formData: this.formData,
      };

      let _this = this;
      _this.$axios
        .post(
          "/SiteCheckTask/SaveCheckTaskTemplateDetails?objCode=" +
          _this.objCode +
          "&companyCode=" +
          _this.CompanyCode +
          "&id="+
          _this.id
          ,
          _this.formData
        )
        .then((res) => {
          _this.$toast("保存成功");
          _this.loading = false;
          setTimeout(() => {
            _this.$router.goRefresh();
          }, 500);
        })
        .catch((err) => {
          console.log("err", err);
        });
    },
    // beforeSearch() {
    //   let _this = this;
    //   _this.$axios
    //     .get("/PlanEmergencyResourceCheck/GetCheckEntity", {
    //       params: {
    //         positionCode: this.$route.query.positionCode,
    //       },
    //     })
    //     .then((res) => {
    //       _this.tabData[0].params.ercCode = res.data.ERCCode;
    //       _this.id = res.data.ID;
    //       _this.$refs.eagleList.getList();
    //     });
    // },
    setCurrentIndex(index) {
      this.currentIndex = index;
      this.showDangerTypeDialog = true;
    },
    setCurrentIndex2(index) {
      this.currentIndex = index;
      this.userChooseDialogShow = true;
      // this.showDepartmentAndUserDialog = true
    },
    // 隐患分类回调
    onDangerTypeCallback(item) {
      if (item) {
        this.showDangerTypeDialog = false;
        var currentIndex = this.currentIndex;
        // this.list[currentIndex].LGDCode = item.DCode;
        // this.list[currentIndex].LGDName = item.DName;
        // this.list[currentIndex].LGDType = item.DType;
        // this.list[currentIndex].LGDTypeName = item.DTypeName;
        // this.list[currentIndex].LGDFullName = item.DFullName;

        this.list[currentIndex].TypeCode=item.TypeCode
        this.list[currentIndex].TypeName=item.TypeName
        this.list[currentIndex].TopTypeCode=item.TopTypeCode
        this.list[currentIndex].TypePNo=item.TypePNo
        this.list[currentIndex].TypeFullName=item.TypeFullName
      }
    },
    // 选择部门人回调
    onUserCallback(item) {
      var index = this.currentIndex;
      if (this.isSimple) {
        this.data.CorrectiveChnName = item[0].name || "";
        this.data.CorrectiveUserName = item[0].code || "";
        this.data.CorrectiveDeptName = "";
        this.data.CorrectiveDeptCode = "";
      } else {
        this.data.CorrectiveDeptName = item.OrgName || "";
        this.data.CorrectiveDeptCode = item.OrgCode || "";
        this.data.CorrectiveChnName = item.ChnName || "";
        this.data.CorrectiveUserName = item.UserName || "";
      }
      // 整改部门/人数据回显
      if (this.data.CorrectiveDeptCode) {
        if (this.data.CorrectiveUserName) {
          this.list[index].correctiveFullName =
            this.data.CorrectiveDeptName + "-" + this.data.CorrectiveChnName;
        } else {
          this.list[index].correctiveFullName = this.data.CorrectiveDeptName;
        }
      } else {
        this.list[index].correctiveFullName = this.data.CorrectiveChnName;
      }
      this.showDepartmentAndUserDialog = false;
      this.showUserChooseDialog = false;
    },
    //人员选择
    userChooseCallback(userlist) {
      var index = this.currentIndex;
      console.log(userlist);

      if (userlist) {
        if (userlist.length > 1) {
          const code = userlist.map((item) => {
            return item.code;
          });
          const name = userlist.map((item) => {
            return item.name;
          });
          this.list[index].CorrectiveChnName = name.join(",");
          this.list[index].CorrectiveUserName = code.join(",");
        } else {
          this.list[index].CorrectiveChnName = userlist.name;
          this.list[index].CorrectiveUserName = userlist.code;
        }
      }
      this.userChooseDialogShow = false;
    },
    dueRiskLeve(riskleve) {
      if (riskleve == "1") {
        return "低风险";
      } else if (riskleve == "2") {
        return "中风险";
      } else if (riskleve == "3") {
        return "高风险";
      }
    },
  },

};
</script>

<style lang="less" scoped>
.dep_sort {
  img {
    height: 15px;
    width: 15px;
    margin-right: 5px;
  }
}

.inconformity-list {
  background-color: #fff;
  margin-top: 10px;
  padding-top: 10px;
}

.bottom-buttons {
  width: 100%;
  position: fixed;
  bottom: 0;
  left: 0;

  .submit-button {
    width: 50%;
    float: left;
    border: none;
  }

  .save-btn {
    width: 50%;
    float: left;
    border: none;
  }
}

.submit-check {
  width: 100%;
  height: 35px;
  margin-top: 10px;
}
</style>
